package com.wg.elasticsearch.job;

import ch.qos.logback.classic.Logger;
import com.alibaba.fastjson.JSONException;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wg.elasticsearch.constant.ElasticsearchConstants;
import com.wg.elasticsearch.model.ShowContent;
import com.wg.elasticsearch.service.ElasticsearchService;
import com.wg.elasticsearch.service.ShowContentService;
import com.wg.elasticsearch.util.DateUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by mc on 18/6/22.
 */
@Component
public class EsSyncJob {

    @Autowired
    private ElasticsearchService elasticsearchService;

    @Autowired
    private ShowContentService showContentService;

    private final Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());

    @Scheduled(cron = "0/20 * * * * ? ")
    public void  syncEs(){

        /**
         * 1、如果不存在则创建索引
         * 2、获取最新更新时间
         * 3、获取需要更新的es数据
         * 4、查看es是否存在id，不存在直接插入，存在直接更新
         */
        try {
            //1
            logger.info("开始同步数据");
            elasticsearchService.createIndex( ElasticsearchConstants.ES_JEST_INDEX_NAME);
            String startDate = elasticsearchService.getMaxUpdateTime();
            Date endDate = new Date();
            Map map = Maps.newHashMap();
            map.put("startDate", DateUtil.str2Date(startDate));
            map.put("endDate",endDate);
            //2
            List<ShowContent> showContents = showContentService.getByDte(map);
            if(showContents.size() <= 0){
                throw new JSONException("不存在需要更新的数据");
            }
            List<ShowContent> insert = Lists.newArrayList();
            List<ShowContent> update = Lists.newArrayList();
            for(ShowContent showContent : showContents){
                ShowContent showContentGet = elasticsearchService.get(ElasticsearchConstants.ES_JEST_INDEX_NAME,ElasticsearchConstants.ES_JEST_TYPE_NAME,showContent.getId().toString());
                if(showContentGet != null){
                    update.add(showContent);
                }else {
                    insert.add(showContent);
                }
            }
            elasticsearchService.createMappingPushData(ElasticsearchConstants.ES_JEST_INDEX_NAME,ElasticsearchConstants.ES_JEST_TYPE_NAME,elasticsearchService.getSourceName(),getList(insert));
            elasticsearchService.update(ElasticsearchConstants.ES_JEST_INDEX_NAME,ElasticsearchConstants.ES_JEST_TYPE_NAME,update);
            logger.info("同步数据结束");
        }catch (Exception e){
            logger.error(e.getMessage());
        }
    }

    /**
     * 获取object
     * @param list
     * @return
     */
    public List<Object> getList(List<ShowContent> list){
        List<Object> list2 = new ArrayList<>();
        for(ShowContent showContent : list){
            list2.add(showContent);
        }
        return list2;
    }
}
